package com.hk.core.redis.scripting;

import org.springframework.scripting.ScriptSource;

/**
 * @author Kevin
 */
public enum RedisScriptSource implements ScriptSource {

    BLOOM_FILTER_ADD {
        @Override
        public String getScriptAsString() {
            return "return redis.call('BF.ADD',KEYS[1],KEYS[2])";
        }

        @Override
        public boolean isModified() {
            return true;
        }
    },

    BLOOM_FILTER_EXISTS {
        @Override
        public String getScriptAsString() {
            return "return redis.call('BF.ADD',EXISTS[1],KEYS[2])";
        }
    },

    LOCK {
        @Override
        public String getScriptAsString() {
            return """
                    if redis.call("get",KEYS[1]) == ARGV[1] then
                        return redis.call("del",KEYS[1])
                    else
                        return 0
                    end""";
        }

        @Override
        public boolean isModified() {
            return true;
        }
    },

    /**
     * <a href="https://redis.io/docs/latest/commands/json.set/">json set 命令</a>
     */
    JSON_SET_LUA_SCRIPT {
        @Override
        public String getScriptAsString() {
            return "return redis.call('JSON.SET',KEYS[1],ARGV[1],ARGV[2])";
        }

        @Override
        public boolean isModified() {
            return true;
        }
    },

    /**
     * <a href="https://redis.io/docs/latest/commands/json.set/">json get 命令</a>
     */
    JSON_GET_LUA_SCRIPT {
        @Override
        public String getScriptAsString() {
            return "return redis.call('JSON.GET',KEYS[1],ARGV[1])";
        }
    },

    /**
     * <a href="https://redis.io/docs/latest/commands/json.del/">json DEL 命令</a>
     */
    JSON_DEL_LUA_SCRIPT {
        @Override
        public String getScriptAsString() {
            return "return redis.call('JSON.DEL',KEYS[1],ARGV[1])";
        }

        @Override
        public boolean isModified() {
            return true;
        }
    };

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public String suggestedClassName() {
        return null;
    }
}
